<?php

namespace process;

use Workerman\Crontab\Crontab;

class Scheduler
{
    /**
     * @return void
     */
    public function onWorkerStart()
    {
        // 每秒钟执行一次
        $this->crontab('*/1 * * * * *', function () {
            echo date('Y-m-d H:i:s') . "\n";
        });
    }

    /**
     * @param string $rule
     * @param callable|string $callback
     * @param string $name
     * @return Crontab
     */
    protected function crontab(string $rule, $callback, string $name = ''): Crontab
    {
        if (!is_callable($callback)) {
            if (class_exists($callback)) {
                if (empty($name)) {
                    $name = $callback;
                }

                $callback = [$callback, 'handle'];
            }
        }

        return new Crontab($rule, $callback, $name);
    }
}
